package org.ricks.game;

import org.ricks.game.handler.WSHandler;
import org.ricks.game.rpc.GameRpcHandler;
import org.ricks.game.rpc.RpcContext;
import org.ricks.ioc.RicksContext;
import org.ricks.net.Protocol;
import org.ricks.net.action.parser.ProtocolParserEnum;
import org.ricks.net.action.protocol.DefInnerProtocol;
import org.ricks.net.action.rpc.RpcClient;
import org.ricks.net.action.rpc.RpcSessionManager;
import org.ricks.net.http.restful.RestfulBootstrap;
import org.ricks.net.http.server.HttpBootstrap;
import org.ricks.net.http.server.handler.WebSocketRouteHandler;
import org.ricks.net.transport.AioQuickClient;
import org.ricks.net.transport.AioQuickServer;
import org.ricks.net.transport.AioSession;

import java.io.IOException;
import java.net.StandardSocketOptions;
import java.util.concurrent.Executors;

public class GameStarterA {

    public static void main(String[] args) throws IOException {
        RicksContext.me().init();
        //1. 实例化路由Handle
        WebSocketRouteHandler routeHandle = new WebSocketRouteHandler();

        //2. 指定路由规则以及请求的处理实现
        routeHandle.route("/", new WSHandler());

        // 3. 启动对外服务
        HttpBootstrap bootstrap = new HttpBootstrap();
        bootstrap.configuration().setWsIdleTimeout(5000);
        bootstrap.webSocketHandler(routeHandle);
        bootstrap.setPort(9000);
        bootstrap.start();


        RestfulBootstrap httpBootstrap = RestfulBootstrap.getInstance();
        httpBootstrap.setAsyncExecutor(Executors.newCachedThreadPool());
        httpBootstrap.bootstrap().configuration().debug(false);
        httpBootstrap.bootstrap().setPort(9090).start();


        GameRpcHandler rpcHandler = new GameRpcHandler();
        AioQuickClient aioQuickClient = new AioQuickClient("localhost", 8888, (Protocol)new DefInnerProtocol(ProtocolParserEnum.STRING.getParser()), rpcHandler);
        aioQuickClient.setReadBufferSize(64 * 1024)
                .setWriteBuffer(128 * 1024, 4096) // 64KB/块, 总容量256MB
                .setOption(StandardSocketOptions.SO_SNDBUF, 256 * 1024)
                .setOption(StandardSocketOptions.SO_RCVBUF, 256 * 1024)
                .disableLowMemory();
//                .setBufferPagePool(new BufferPagePool(256 * 1024 * 1024, 8192));
        AioSession session = aioQuickClient.start();

        RpcClient rpcClient = RpcSessionManager.getOrCreateClient(session);
        RpcContext.re(1, rpcClient);
    }
}
